package com.justgame.project.cloud.base.entity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
@Getter
@Setter
public class PageRequest<T extends BaseTable> {
    private static final Long DEFAULT_PAGE = 1L;
    private static final Long DEFAULT_SIZE = 10L;

    private static final Long MAX_SIZE = 1000L;


    private Long page;
    private Long size;
    private String orderBy;
    private Long total;
    private List<T> rows;

    public PageRequest() {
        checkVal();
    }

    public PageRequest(long page, long size, String orderBy) {
        this.page = page;
        this.size = size;
        this.orderBy = orderBy;
        checkVal();
    }

    public IPage<T> build(){
        return Page.of(page,size);
    }

    public <E extends IPage<T>> PageRequest<T> returnRows(E page) {
        this.rows = page.getRecords();
        this.size = page.getSize();
        this.page = page.getCurrent();
        this.total = page.getTotal();
        return this;
    }

    public PageRequest<T> returnRows(List<T> data, Long totalCount) {
        this.total = totalCount;
        this.rows = data;
        return this;
    }

    public PageRequest<T> returnRows(List<T> data) {
        this.rows = data;
        return this;
    }

    private void checkVal() {
        if (this.page == null || this.page <= 0) this.page = DEFAULT_PAGE;
        if (this.size == null || this.size <= 0) this.size = MAX_SIZE;
    }

    public void startPage(){

    }

}
